package com.wali.modules.common.service;

import com.wali.common.ccp.execl.CcpExcelReport;
import com.wali.modules.common.bo.ComGoodsBO;
import com.wali.modules.common.dao.IComGoodsDao;
import com.wali.modules.common.vo.ComGoodsVO;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author walixu
 * @ClassName com.wali.modules.common.service
 * @Description 备案商品service类
 * @email wali.xu@kpmg.com
 * @date 09/03/2019 8:16 下午
 */
@Service
public class ComGoodsService {

    @Autowired
    private IComGoodsDao iComGoodsDao;

    /**
     * @param
     * @return
     * @throws
     * @Title importComGoods
     * @Description 导入商品信息
     */
    @Transactional(rollbackFor = Exception.class)
    public String importComGoods(List<ComGoodsBO> comGoodsList) {
        List<ComGoodsVO> comGoodsVOList = new ArrayList<>();
        for (ComGoodsBO comGoods : comGoodsList) {
            comGoodsVOList.add(ComGoodsBO.convert(comGoods));
        }
        List<ComGoodsVO> existComGoodsList = iComGoodsDao.listComGoods(comGoodsVOList);

        // 筛选出需要删除的物品信息
        List<ComGoodsVO> deleteComGoods = new ArrayList();// 存放需要删除的商品信息
        Iterator<ComGoodsVO> iterator = comGoodsVOList.iterator();
        while (iterator.hasNext()) {
            ComGoodsVO temp = iterator.next();
            if ("-1".equals(temp.getGoodsName())) {
                deleteComGoods.add(temp);
                iterator.remove();
            }
        }

        // 将已经存在的物品信息转为map
        Map<String, ComGoodsVO> existComGoodsMap = new HashMap();// 存在数据库已经存在的商品信息
        for (ComGoodsVO temp : existComGoodsList) {
            existComGoodsMap.put(temp.getSerialNo(), temp);
        }

        // 新增需要添加的商品信息
        List<ComGoodsVO> updateComGoods = new ArrayList();// 存放需要更新的商品信息
        List<ComGoodsVO> saveComGoods = new ArrayList();// 存放需要保存的商品信息
        for (ComGoodsVO temp : comGoodsVOList) {
            if (existComGoodsMap.containsKey(temp.getSerialNo())) {
                updateComGoods.add(temp);
            } else {
                saveComGoods.add(temp);
            }
        }

        int deleteCount = 0, saveCount = 0, updateCount = 0;
        if (deleteComGoods.size() > 0) {
            deleteCount = iComGoodsDao.batchDeleteComGoods(deleteComGoods);
        }
        if (saveComGoods.size() > 0) {
            saveCount = iComGoodsDao.batchSaveComGoods(saveComGoods);
        }
        if (updateComGoods.size() > 0) {
            updateCount = iComGoodsDao.batchUpdateComGoods(updateComGoods);
        }
        return saveCount + "," + updateCount + "," + deleteCount;
    }

    /**
     * @param
     * @return
     * @throws
     * @Title importComGoods
     * @Description 导出商品信息
     */
    public CcpExcelReport exportComGoods() {
        List<ComGoodsVO> comGoodsList = iComGoodsDao.listAllComGoods();

        String[] header = new String[]{"客户ID", "商品序列号",
                "海关商品编码", "品名", "品名（英文）", "品牌", "规格型号", "原产国（地区）", "计量单位",
                "商品净重（kg）", "商品毛重（kg）", "单价/公斤", "商品单价/货物的单价", "商品币制"};
        CcpExcelReport excelReport = new CcpExcelReport(null, header);

        if (comGoodsList.size() > 0) {
            int index = 0;
            for (final ComGoodsVO temp : comGoodsList) {
                index++;
                if (index <= 1000) {
                    Row row = excelReport.addRow();
                    excelReport.addCell(row, 0, temp.getUserNo());
                    excelReport.addCell(row, 1, temp.getSerialNo());
                    excelReport.addCell(row, 2, temp.getCustomerGoodsId());
                    excelReport.addCell(row, 3, temp.getGoodsName());
                    excelReport.addCell(row, 4, temp.getGoodsEnname());
                    excelReport.addCell(row, 5, temp.getBrand());
                    excelReport.addCell(row, 6, temp.getSpecs());
                    excelReport.addCell(row, 7, temp.getOrginCountry());
                    excelReport.addCell(row, 8, temp.getUnit());
                    excelReport.addCell(row, 9, temp.getGoodsWeight());
                    excelReport.addCell(row, 10, temp.getTotalWeight());
                    excelReport.addCell(row, 11, temp.getPricekg());
                    excelReport.addCell(row, 12, temp.getPriceGoods());
                    excelReport.addCell(row, 13, temp.getCurrency());
                }
            }
        }

        return excelReport;
    }

}
